<?php

declare(strict_types=1);

namespace Common\trait;

use App\Exception\BusinessException;
use App\Http\Common\ResultCode;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

trait ValidatorTrait
{
    public function validate(array $data, array $rules, array $message): array
    {
        $factory = make(ValidatorFactoryInterface::class);
        $validator = $factory->make($data, $rules, $message);
        if ($validator->fails()) {
            $message = $validator->errors()->first();
            throw new BusinessException(ResultCode::UNPROCESSABLE_ENTITY, $message);
        }
        return $data;
    } 
}